package com.example.demo.service.impl;

import com.example.demo.dao.RepairDao;
import com.example.demo.entity.response.Response;
import com.example.demo.entity.sqlbean.Repair;
import com.example.demo.entity.sqlbean.RepairExpressNo;
import com.example.demo.entity.sqlbean.RepairList;
import com.example.demo.entity.sqlbean.repair.ProductRepair;
import com.example.demo.entity.sqlbean.repair.ProjectApplicationRepair;
import com.example.demo.entity.sqlbean.repair.ProjectDeviceRepair;
import com.example.demo.entity.sqlbean.repair.RolesEmployees;
import com.example.demo.service.RepairService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {


    @Resource
    RepairDao repairDao;

    @Override
    public Response insertRepair(Repair repair) {
        Response response = new Response();
        Long projectId = repair.getProjectId();
        for (int i = 0; i < repair.getProductList().size(); i++) {
            int total = repair.getProductList().get(i).getTotalNumber();
            RepairList repairList = repairDao.selectProjectProduct(projectId,
                    repair.getProductList().get(i).getProductId());
            if (repairList == null){
                response = Response.error(4002,"无该产品");
                return response;
            }
            else  if (repairList.getTotalNumber() < total){
                response = Response.error(4001,(i+1)+"设备数量大于项目中数量");
                return  response;
            }
        }
        long id = repairDao.insertApproval(repair);
        if (id ==0){
            response = Response.error(4002,"申请错误");
            return  response;
        }
        id = repair.getId();
        for (int i = 0; i < repair.getProductList().size(); i++) {
            int result = repairDao.saveNewProjectMaintenance(id,repair.getProductList().get(i).getProductId(),
                    repair.getProductList().get(i).getTotalNumber());
            if (result !=1 ){
                response = Response.error(4003,"插入失败");
                return response;
            }
        }
        response =Response.success();
        return response;
    }

    @Override
    public Response selectRepair(Long approvalId) {
        Response response = new Response();
        ProductRepair productRepair = new ProductRepair();

        List<ProjectDeviceRepair> projectDeviceRepair =  repairDao.selectProjectProductById(approvalId);
        if (projectDeviceRepair.isEmpty()){
            response =Response.error(4004,"无报修产品查询");
            return response;
        }
        productRepair.setProductRepair(projectDeviceRepair);

        ProjectApplicationRepair projectApplicationRepair = repairDao.selectProjectApproval(approvalId);
        if ("".equals(projectApplicationRepair)){
            response =Response.error(4005,"无该申请");
            return response;
        }
        Long rolesId = projectApplicationRepair.getRolesId();
        productRepair.setProjectApplicationRepairs(projectApplicationRepair);

        if (rolesId != null){
            RolesEmployees rolesEmployees = repairDao.selectRolesEmployees(rolesId);
            if ("".equals(rolesEmployees)){
                response = Response.error(4006,"工程师错误");
                return response;
            }
            productRepair.setRolesEmployees(rolesEmployees);
        }

        response = Response.success();
        response = Response.success(productRepair);

        return response;
    }

    @Override
    public Response selectRepairExpressNo(Long approvalId) {
        Response response = new Response();
        List<RepairExpressNo> repairExpressNoList = repairDao.selectExpressNo(approvalId);
        if (repairExpressNoList.isEmpty()) {
            response = Response.error(4007, "申请请求失败");

        }else {
            response = Response.success();
            response = Response.success(repairExpressNoList);
        }
        return response;
    }
}
